const app = getApp();
var interval = null //倒计时函数
Page({
  data: {
    phoneNumber: '',
    phone4:'',
    code:'',
    userName:'',
    time: '获取验证码',
    currentTime: 61
  },
  onShow:function(){
    this.loadInfo();
  },
  onLoad:function(options){
    this.loadInfo();
  },
  loadInfo: function (){
    // 用户信息
    const _this = this;
    let baseInfo = app.globalData.baseInfo;
    _this.setData({
      phoneNumber: baseInfo.phoneNumber,
      userName: baseInfo.userName,
      phone4: baseInfo.phoneNumber.substring(7, 11)
    })
  },
  //得到code
  getcode:function(e){
    this.setData({
      code:e.detail.value
    })
  },
  toNext: function (e) {
    console.log(e.detail.value.number);
    const _this = this;
    if (this.data.securityCode){
      wx.request({
        url: app.globalData.path + 'services/app/applets/ValidatePhone',
        data: {
          code: e.detail.value.number,
          securityCode: _this.data.securityCode
        },
        method: 'POST',
        header: {
          'content-type': 'application/json', // 默认值
          'Authorization': 'Bearer ' + app.globalData.authorization
        },
        success: function (res) {
          console.log(res.data)
          if(res.data.success){
            wx.navigateTo({
              url: '../next/next'
            })
          } else {
            wx.showToast({
              title: res.data.error.message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }else{
      wx.showModal({
        content: '请填写验证码',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
    }
  },
  sendCode:function(){
    const _this = this;
    this.settimeD();
    this.setData({
      disabled: true
    })
    wx.request({
      url: app.globalData.path + 'services/app/applets/SendSMSCode',
      data: {
        phoneNumber: _this.data.userName,
        SendType: 5
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data)
        console.log(res.data.result.securityCode)
        if (res.data.success) {
          _this.setData({
            securityCode: res.data.result.securityCode
          })
          if (res.data.result.sendResultType == 2) {
            wx.showToast({
              title: '手机号不存在',
              icon: 'none',
              duration: 2000
            })
          } else if (res.data.result.sendResultType == 3) {
            wx.showToast({
              title: '操作频繁',
              icon: 'none',
              duration: 2000
            })
          } else if (res.data.result.sendResultType == 4 || res.data.result.sendResultType == 5) {
            wx.showToast({
              title: '短信模板错误',
              icon: 'none',
              duration: 2000
            })
          }
        } else {
          wx.showToast({
            title: '发送失败，请重试',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  //倒计时
  settimeD: function (options) {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime: 61,
          disabled: false
        })
      }
    }, 1000)
  },
})